function ModulesAssistant() {
}

ModulesAssistant = Class.create({
	setup: function() {
		this.setupModules();

		//About
		this.controller.setupWidget(Mojo.Menu.appMenu, {
			omitDefaultItems: true
		}, {
			visible: true,
			items: [
      		Mojo.Menu.editItem,
			{ label: "Disclaimer", command: "do-disclaimer" },					
				{ label: "License", command: "do-license" },
				{ label: "Help", command: "do-help" }
			]
		});


		
		this.controller.setupWidget("modulesList", {
			itemTemplate: "modules/moduleItem",
			dividerTemplate: "modules/divider",
			filterFunction: this.searchList.bind(this),
			dividerFunction: this.moduleCategory,
			delay: 150
		}, this.historyModel = {});
		this.controller.listen("modulesList", Mojo.Event.listTap, this.gotoModule.bind(this));

	},
	
	openMenu: function(event, category) {
		this.controller.popupSubmenu({
			onChoose: function(command) {
				var module = this.modules.find(function(f) { return f.command == command; });
				this.controller.stageController.pushScene(command, module.label);
			}.bind(this),
			placeNear: $(category),
			items: this.modules
		});
	},

	setupModules: function() {
		//var	selection = this.categoryModel.value;
		// define modules according to filter selection
		this.modules = [
			{label: "6CIT cognitive test", scene: "sixcit", category: "general" },
			{label: "Abbreviated Mental Test", scene: "amt", category: "general" },
			{label: "Apgar score", scene: "apgar", category: "general" },
			{label: "BSA/BMI/IBW", scene: "bsa", category: "general" },
			{label: "CURB65 Pneumonia score", scene: "curb65", category: "general" },
			{label: "Harris-Benedict Cal Req", scene: "harris", category: "general" },
			{label: "MELD Score (Liver Failure)", scene: "meld", category: "general" },
			{label: "Pregnancy Due Date", scene: "preg-due", category: "general" },
			{label: "Ranson Criteria (Pancreatitis)", scene: "ranson", category: "general" },
			{label: "Corrected QT Interval (QTc)", scene: "qtc", category: "cardiology" },
			{label: "TIMI Score for NSTEMI/UA", scene: "timinstemi", category: "cardiology" },
			{label: "TIMI Score for STEMI", scene: "timistemi", category: "cardiology" },
			{label: "Alveolar-arterial gradient", scene: "aa", category: "icu" },
			{label: "Apache II (ICU Mortality)", scene: "apache", category: "icu" },
			{label: "Glasgow Coma Scale", scene: "glasgow", category: "icu" },
			{label: "Anion gap", scene: "anion", category: "renal" },
			{label: "Correc Ca (for hypoalbuminemia)", scene: "cca", category: "renal" },
			{label: "Correc Na (for hyperglycemia)", scene: "correna", category: "renal" },
			{label: "Cockroft-Gault GFR (Cr Clearance)", scene: "gfr", category: "renal" },
			{label: "Fractional Excretion Na", scene: "frena", category: "renal" },
			{label: "Fractional Excretion Urea", scene: "freurea", category: "renal" },
			{label: "Free Water Deficit", scene: "fwd", category: "renal" },
			{label: "MDRD GFR (Cr Clearance)", scene: "mgfr", category: "renal" }
		];
	},

	searchList: function(filterString, listWidget, offset, count) {
		var filter = filterString.toLowerCase();
		var items = [];
		for(var i = 0; i < this.modules.length; i++) {
			var item = this.modules[i];
			var name = item.label;
			if(name.toLowerCase().indexOf(filter) != -1) {
				items.push(item);
			}
		}
		listWidget.mojo.noticeUpdatedItems(offset, items);
		listWidget.mojo.setLength(items.length);
		listWidget.mojo.setCount(items.length);
	},

	gotoModule: function(event) {
		var module = event.item;
		this.controller.stageController.pushScene(module.scene, module.label);
	},
	
	moduleCategory: function(item) {
		return item.category;
	}
});
